package juc.design.synchronization;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.MonitorService")
public class MonitorService {

    //用来表示是否有线程已经在执行启动了
    private volatile boolean starting;

    public void start(String threadName){
        log.debug("线程{}尝试启动监控线程。。。",threadName);
        synchronized (this){
            if(starting){
                log.debug("监控系统已启动。。。");
                return;
            }
            starting = true;
            log.debug("线程{}启动监控系统成功",threadName);
        }
    }

    public static void main(String[] args) {
        MonitorService monitorService = new MonitorService();

        Thread t1 = new Thread(()->{
            monitorService.start("t1");
        },"t1");
        Thread t2 = new Thread(()->{
            monitorService.start("t2");
        },"t2");

        t1.start();
        t2.start();
    }
}
